<?php


namespace Frieza\Modules\RiskContent\Actions;

use Doraemon\cache\BlackList;
use Doraemon\cache\EsUpdateCollector;
use Doraemon\cache\Queue;
use Doraemon\cache\SolrUpdateCollector;
use Doraemon\model\AdmOperateLog;
use Doraemon\model\AdmRecommendUser;
use Doraemon\model\AdmToBeMarked;
use Doraemon\model\AdmUserType;
use Doraemon\model\BestUser;
use Doraemon\model\Member;
use Doraemon\service\admin\AdmUserFilter;
use Doraemon\service\search\AdmUserSearch;
use Doraemon\service\solr\SolrUserMark;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Doraemon\service\Themis\Log as ThemisLog;
use Doraemon\model\Themis\Log as ThemisLogModel;

class AddUserB1 extends RowAction
{
    public $name = '加入B1';
    const ALERT_TOAST = "你确定要加入B1吗";
    public static $user_quality = ['good' => 1, 'normal' => 2, 'S2' => 3, 'B2' => 9, 'B1' => 10, 'B3' => 11];

    public function handle($uid,Request $request)
    {
        //获取运营级用户质量
        $qualityOld = (int)self::getUserQuality($uid);
        $userTypeOld = AdmUserType::getByUid($uid);
        $admin_id = Admin::user()->id;
        $admin_name = Admin::user()->username;
        $quality = AdmUserType::B1; //B1用户
        $update_data = [
            'quality' => $quality,
            'addtime' => time(),
            'admin_id'=> $admin_id,
        ];
        // 添加themis日志
        $aOpt = array(
            'admin_id'    => $admin_id,
            'remark'      => "该用户账号已被列为B1",
            'status'      => ThemisLogModel::STATUS_BLOCKED,
            'action_type' => ThemisLogModel::ACTION_TYPE_ADD_B1,
            'illegal_or_punish' => ThemisLogModel::ACTION_PUNISH,
        );
        $aThemisLog = ThemisLog::fetchThemisLogData($uid, $aOpt);
        ThemisLog::add($aThemisLog);
        //处理优质用户的相关逻辑
        if (!in_array($qualityOld, [false, 3])){
            if ($qualityOld != $quality){
                // 更新|删除老的用户质量数据
                if ($qualityOld == self::$user_quality['S2']){
                    $judge = self::delBestUser($uid);
                    if ($judge) {
                        $rs = AdmUserType::updateInfo($uid,$update_data);
                    }
                }elseif (in_array($qualityOld, [self::$user_quality['B1'], self::$user_quality['B2'], self::$user_quality['B3'], self::$user_quality['normal'], self::$user_quality['good']])){
                    $rs = AdmUserType::updateInfo($uid,$update_data);
                }
                if (!$rs) {
                    return $this->response()->error('操作失败!!');
                }
            }else{
                $rs = AdmUserType::updateInfo($uid,$update_data);
            }
        }else {
            $rs = AdmUserType::getByUid($uid);
            if ($rs) {
                $rs = AdmUserType::updateInfo($uid, $update_data);
            } else {
                $user_addtime = Member::getOne($uid)['addtime'];
                $update_data['addtime'] = $user_addtime;
                $rs = AdmUserType::save($uid, $update_data);
            }
        }
        Queue::insertQueue('Photo/reindex_photo_solr', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        SolrUpdateCollector::insert($uid);
        EsUpdateCollector::insert($uid);
        if (empty($rs)){
            return $this->response()->error('出错啦,稍后重试!!');
        }else{
            if ($quality && $quality == self::$user_quality['B1']){
                BlackList::addUid2BlackList($uid);
                //B1用户内容更新 事件化实现
//                Queue::insertQueue('UserB1/setStatusByUid', array(
//                    'uid' => $uid,
//                    "type"=>"set_b1"
//                ), Queue::QUEUE_COMMON);
            }

            if ($quality && $quality != self::$user_quality['B1']){
                BlackList::delUid4BlackList($uid);
                //取消B1内容更新  事件化实现
//                Queue::insertQueue('UserB1/setStatusByUid', array(
//                    'uid' => $uid,
//                    "type"=>"cancel_b1"
//                ), Queue::QUEUE_COMMON);
            }

        }
        if ($quality) {
            AdmToBeMarked::deleteByUid($uid);
        }
        AdmUserFilter::updateUserQuality($uid);
        AdmUserSearch::updateAdmUserSearchByUid($uid);

        AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['edit_user_quality'],$admin_id,$admin_name,$qualityOld,$quality);
        return $this->response()->success('操作成功')->refresh();
    }

    //获取运营标识信息
    private static function getUserQuality($uid){
        if (BestUser::checkBest($uid)){
            return self::$user_quality['S2'];
        }
        //是否在灰(B2)，黑名单(B1)里
        $qualityOld = AdmUserType::getByUid($uid);
        if ($qualityOld) {
            return $qualityOld['quality'];
        } else {
            return false;
        }
    }
    //移除用户S2信息
    private static function delBestUser($uid){
        // 删除用户在推荐表的数据
        AdmRecommendUser::deleteByUid($uid);
        // 删除精华表数据
        BestUser::deleteUser($uid);
        return true;
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::ALERT_TOAST);
    }
}